﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class admin_edmain : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        connection con = new connection();
        SqlConnection cn = con.connect();
        SqlCommand mySqlSelect = new SqlCommand();
        mySqlSelect.Connection = cn;
        mySqlSelect.CommandText = "select * from main where id = @id and lang = @lang";
        mySqlSelect.CommandType = CommandType.Text;
        mySqlSelect.Parameters.AddWithValue("id", Request.QueryString["id"] != null ? Request.QueryString["id"].ToString() : "");
        mySqlSelect.Parameters.AddWithValue("lang", Request.QueryString["lang"] != null ? Request.QueryString["lang"].ToString() : "");
        try
        {
        SqlDataReader rd = mySqlSelect.ExecuteReader();
            if (rd.Read())
            {
                 me.Text = !rd.IsDBNull(2) != null ? rd.GetString(2).ToString() : "";
                title.Text = !rd.IsDBNull(3) != null ? rd.GetString(3).ToString() : "";
                nd.Value = !rd.IsDBNull(4) ? rd.GetString(4).ToString() : "";
                des.Text = !rd.IsDBNull(5) != null ? rd.GetString(5).ToString() : "";
            }
            rd.Close();
            cn.Close();
        }
        catch (Exception ex)
        {
            cn.Close();
            Console.WriteLine("Loi setValueFromDatabaseToForm : " + ex);
        }
    }

    protected void addbt_Click(object sender, EventArgs e)
    {
        connection con = new connection();
        SqlConnection cn = con.connect();
        SqlCommand mySqlSelect = new SqlCommand("update main set menu = @menu, title = @title, [content] = @cont, description = @des where id = @id and lang = @lang", cn);
        mySqlSelect.CommandType = CommandType.Text;

        SqlParameter menu = new SqlParameter("@menu", SqlDbType.NVarChar, 50);
        menu.Value = me.Text;

        SqlParameter tit = new SqlParameter("@title", SqlDbType.NVarChar, 100);
        tit.Value = title.Text;

        SqlParameter cont = new SqlParameter("@cont", SqlDbType.NVarChar);
        cont.Value = nd.Value;

        SqlParameter desc = new SqlParameter("@des", SqlDbType.NVarChar, 150);
        desc.Value = des.Text;

        SqlParameter id = new SqlParameter("@id", SqlDbType.BigInt);
        id.Value = Request.Params["id"];

        SqlParameter lang = new SqlParameter("@lang", SqlDbType.NVarChar, 2);
        lang.Value = Request.Params["lang"];

        mySqlSelect.Parameters.Add(menu);
        mySqlSelect.Parameters.Add(tit);
        mySqlSelect.Parameters.Add(cont);
        mySqlSelect.Parameters.Add(desc);
        mySqlSelect.Parameters.Add(id);
        mySqlSelect.Parameters.Add(lang);
        mySqlSelect.ExecuteNonQuery();
        cn.Close();
        lbInfo.Visible = true;
        lbInfo.Text = "Bạn đã cập nhật thành công";
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx?page=qlmain");
    }
}